import { Injectable } from '@nestjs/common';
import { DataSource } from 'typeorm';
import { userInfo } from './sync/userInfo.entity';
import { addressInfo } from './addressInfo.entity';
import { commodityInfo } from './commodityInfo.entity';
import { orderInfo } from './orderInfo.entity';
import { userInfoCenterSync } from './userInfoCenterSync.entity';
import * as crypto from 'crypto';
import { userInfo as userInfoBen } from './userInfo.entity';

@Injectable()
export class AppService {
  constructor(private dataSource: DataSource) {}



  async getAddressInfo():Promise<any>{
    const res = await this.dataSource.manager.find(addressInfo);
    return res;
  }

  // 修改密码 service
  async revise(body: any, username: any): Promise<any> {
    let revisePassword = body.revisePassword;
    let hmac = crypto.createHmac('sha1', 'salt');
    console.log(body.password);
    let password = hmac.update(body.password).digest().toString('base64');
    console.log(username)
    console.log(password)
    const userOne = await this.dataSource.manager.findOneBy(userInfoBen, {
      name: username,
      password: password,
      flag: '1',
    });
    if (userOne == null) {
      return {
        code: 1,
        msg: '密码错误',
      };
    } else {
      hmac = crypto.createHmac('sha1', 'salt');
      revisePassword = hmac.update(revisePassword).digest().toString('base64');
      const res = await this.dataSource.manager.update(userInfoBen,{
        name: username
      }, {
        password: revisePassword
      })
      return res;
    }
  }

  async sync(): Promise<string> {
    const AppDataSource = new DataSource({
      type: 'mongodb',
      host: '47.115.220.9',
      port: 27017,
      database: 'shop',
      entities: [addressInfo, userInfo, commodityInfo, orderInfo],
    });

    AppDataSource.initialize()
      .then(async () => {
        let address = await AppDataSource.manager.find(addressInfo);
        for (let i = 0; i < address.length; i++) {
          const addressTmp = new addressInfo();
          addressTmp._id = address[i]._id;
          addressTmp.userId = address[i].userId;
          addressTmp.address = address[i].address;
          addressTmp.phone = address[i].phone;
          addressTmp.name = address[i].name;
          addressTmp.flag = address[i].flag;
          addressTmp.date = address[i].date;
          this.dataSource.manager.save(addressTmp).then((res) => {
            console.log(res);
          });
        }
        let user = await AppDataSource.manager.find(userInfo);
        for (let i = 0; i < user.length; i++) {
          const userInfoSync = new userInfoCenterSync();
          userInfoSync._id = user[i]._id;
          userInfoSync.date = user[i].date;
          userInfoSync.flag = user[i].flag;
          userInfoSync.name = user[i].name;
          userInfoSync.password = user[i].password;
          this.dataSource.manager.save(userInfoSync);
        }
        let commodity = await AppDataSource.manager.find(commodityInfo);
        for (let i = 0; i < commodity.length; i++) {
          const commodityTmp = new commodityInfo();
          commodityTmp._id = commodity[i]._id;
          commodityTmp.date = commodity[i].date;
          commodityTmp.flag = commodity[i].flag;
          commodityTmp.name = commodity[i].name;
          commodityTmp.pric = commodity[i].pric;
          commodityTmp.store = commodity[i].store;
          commodityTmp.text = commodity[i].text;
          this.dataSource.manager.save(commodityTmp);
        }
        let order = await AppDataSource.manager.find(orderInfo);
        for (let i = 0; i < order.length; i++) {
          const orderTmp = new orderInfo();
          orderTmp._id = order[i]._id;
          orderTmp.addressId = order[i].addressId;
          orderTmp.date = order[i].date;
          orderTmp.flag = order[i].flag;
          orderTmp.storeId = order[i].storeId;
          orderTmp.userId = order[i].userId;
          this.dataSource.manager.save(orderTmp);
        }
      })
      .catch((error) => console.log(error));
    return '';
  }
}
